﻿namespace KeRui.TemplateMaker
{
    using System;
    using System.Text.RegularExpressions;

    internal class ResolveAttribute
    {
        private static NodeAttribute GetForeachAttribute(string nodeTag)
        {
            Match match = new Regex(@"node(?<nodeid>\d+)(_(?<isincludechild>includechild))?(_(?<ishot>hot))?(_(?<ismarked>marked))?(_(?<isrecommend>recommend))?(_(?<istop>top))?(_(contenttype(?<contenttype>\d+)))?((_(?<order>\w+))(_(?<direction>\w+)))?", RegexOptions.IgnoreCase).Match(nodeTag);
            if (match.Success)
            {
                int num2;
                int nodeID = Convert.ToInt32(match.Groups["nodeid"].ToString());
                bool isIncludeChild = match.Groups["isincludechild"].ToString().ToLower() == "includechild";
                bool isHot = match.Groups["ishot"].ToString().ToLower() == "hot";
                bool isMarked = match.Groups["ismarked"].ToString().ToLower() == "marked";
                bool isRecommend = match.Groups["isrecommend"].ToString().ToLower() == "recommend";
                bool isTop = match.Groups["istop"].ToString().ToLower() == "top";
                if (string.IsNullOrEmpty(match.Groups["contenttype"].ToString()))
                {
                    num2 = 0;
                }
                else
                {
                    num2 = Convert.ToInt32(match.Groups["contenttype"].ToString());
                }
                string orderBy = match.Groups["order"].ToString();
                return new NodeAttribute(nodeID, isHot, isMarked, isTop, isRecommend, isIncludeChild, num2, orderBy, match.Groups["direction"].ToString());
            }
            return new NodeAttribute();
        }

        private static SellerAttribute GetSellerAttribute(string nodeTag)
        {
            Match match = new Regex(@"seller(?:_typename_(?<typename>.*?))?(?:_order_(?<order>\w+)_(?<direction>desc|asc))?(?:_top_(?<top>\d+))?", RegexOptions.RightToLeft | RegexOptions.IgnoreCase).Match(nodeTag);
            return (match.Success ? new SellerAttribute(match.Groups["typename"].ToString(), match.Groups["top"].ToString(), match.Groups["order"].ToString(), match.Groups["direction"].ToString()) : new SellerAttribute());
        }

        private static SellerVocationAttribute GetSellerVocationAttribute(string nodeTag)
        {
            Match match = new Regex(@"node(?<vocationid>\d+)", RegexOptions.RightToLeft | RegexOptions.IgnoreCase).Match(nodeTag);
            return (match.Success ? new SellerVocationAttribute(Convert.ToInt32(match.Groups["vocationid"].Value)) : new SellerVocationAttribute());
        }

        public NodeAttribute ResolveNodeAttribute(string nodeTag)
        {
            return GetForeachAttribute(nodeTag);
        }

        public SellerAttribute ResolveSellerAttribute(string nodeTag)
        {
            return GetSellerAttribute(nodeTag);
        }

        public SellerVocationAttribute ResolveSellerVocationAttribute(string nodeTag)
        {
            return GetSellerVocationAttribute(nodeTag);
        }
    }
}

